package com.zhuhai.service;

import com.zhuhai.entity.TicketInfo;
import com.zhuhai.repository.TicketInfoReposity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 票据服务类
 *  @author zhuhai
 *  @version 1.0.0.0
 */
@Service
public class TicketInfoService {

    @Resource
    private TicketInfoReposity ticketInfoReposity;

    @PersistenceUnit
    private EntityManagerFactory emf;

    /**
     * 获取票据列表
     * @param spec
     * @param pageable
     * @return 结果
     */
    public Page<TicketInfo> getTicketList(final Specification<TicketInfo> spec, final Pageable pageable) {
        return ticketInfoReposity.findAll(spec, pageable);
    }

    /**
     * 使用Criteria 方式统计出每天的出票数
     * @return 结果
     */
    public List<Map> getTicketgroup() {
        final EntityManager entityManager = emf.createEntityManager();
        final List<Tuple> result = createSumQuery(entityManager).getResultList();
        final Iterator iterator = result.iterator();
        final List<Map> list = new ArrayList<>();
        while(iterator.hasNext()){
            final Map map = new HashMap(16);
            final Tuple tuple = (Tuple) iterator.next();
            map.put("segment", tuple.toArray()[0]);
            map.put("num", tuple.toArray()[1]);
            list.add(map);
        }
        return list;
    }

    /**
     *
     * @return
     */
    private TypedQuery<Tuple> createSumQuery(final EntityManager entityManager) {
        final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<Tuple> query = builder.createTupleQuery();
        final Root<TicketInfo> root = query.from(TicketInfo.class);
        /**
         * 这种方式有点不合理
        final Join<Predicate,Segment> join =root.join("segment",JoinType.LEFT);
        final List<Selection<?>> selections = new LinkedList<>();
        selections.add(join.get("id"));
        selections.add(builder.count(join.get("id")).as(Long.class));
        query.multiselect(selections);
        final List<Expression<?>> groupingList = new LinkedList<>();
        groupingList.add(join.get("id"));
        query.groupBy(groupingList);
         */
        final List<Selection<?>> selections = new LinkedList<>();
        selections.add(root.get("segment").get("id"));
        selections.add(builder.count(root.get("segment").get("id")).as(Long.class));
        query.multiselect(selections);
        final List<Expression<?>> groupingList = new LinkedList<>();
        groupingList.add(root.get("segment").get("id"));
        query.groupBy(groupingList);
        return entityManager.createQuery(query);
    }
    /**
     * 获取票据为空的数据
     * @return
     */
    public List<TicketInfo> findByPriceIsNull(){
        return ticketInfoReposity.findByPriceIsNull();
    }

    /**
     * 删除数据
     * @return
     */
    public void  deleteAll(final Iterable<TicketInfo> iterable){
        ticketInfoReposity.deleteAll(iterable);
    }
    /**
     * 删除数据
     * @return
     */
    public void  deleteInBatch(final Iterable<TicketInfo> iterable){
        ticketInfoReposity.deleteInBatch(iterable);
    }
}
